/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.menu.context;

import com.blandware.atleap.webapp.taglib.core.util.ContextMenuItem;

import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>This class implements all common functionality of ItemContainer
 * </p>
 * <p><a href="ItemContainerSupport.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2006/08/03 10:08:25 $
 */
public class ItemContainerSupport extends SimpleTagSupport implements ItemContainer {

	// ~ Instance fields

	/**
	 * List of items to include in menu. Each of them corresponds to row with
     * its index. Each menu is a list of menu items.
	 */
	protected List menus = new ArrayList();

	/**
	 * Creates new instance of ContextMenuTag
	 */
	public ItemContainerSupport() {
	}

	/**
	 * @see ItemContainer#storeItem(Integer, com.blandware.atleap.webapp.taglib.core.util.ContextMenuItem)
	 */
	public void storeItem(Integer index, ContextMenuItem item) {
        // firstly obtain (or create) menu for row with given index
		List menu = new ArrayList();
		int i = index.intValue();
		if ( i < menus.size() ) {
			menu = (List) menus.get(i);
		}
        // add item to that menu
		menu.add(item);
        // update (or add) that menu
		if ( i < menus.size() ) {
			menus.set(i, menu);
		} else {
			menus.add(menu);
		}
	}

}
